import local from "../../../utils/localStorage";

// subcontract/Login/login/login.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    isSelected: false,//是否选中
    showLoginPop: false,//第一次登录  填写用户资料
    avatar: '',
    config: {},//配置
    phoneNumber: ""
  },
  /**获取手机号 */
  async getPhoneNumber(e) {
    console.log(e);
    if (e.detail.code) {
      let objrct = {
        code: e.detail.code,
      }
      const { code, data } = await getApp().api.getPhoneNumber(objrct)
      if (code == 1) {
        this.setData({
          phoneNumber: data.phone_info.phoneNumber
        })
      }
    }
  },
  /**配置项 */
  async config() {
    const { code, data } = await getApp().api.config()
    if (code == 1) {
      getApp().local.set('config', data)
      this.setData({
        config: data
      })
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**更新手机号 */
  async changeMobile(new_mobile) {
    let object = {
      action: 'binding',
      new_mobile,
    }
    const { code, data } = await getApp().api.changeMobile(object)
  },
  /**
   * 更新用户信息 event 
   */
  async updateUser() {
    let object = {
      avatar: this.data.avatar,
      nickname: this.data.nickname
    }
    const { code, data } = await getApp().api.updateUser(object)
    if (code == 1) {
      // getApp().navigateTo('/subcontract/Mine/merchantsSettle/merchants?type=1')
      wx.switchTab({
        url: '/pages/Home/home/home'
      })
      this.changeMobile(this.data.phoneNumber)
    }
  },
  input(event) {
    this.setData({
      nickname: event.detail.value
    })
  },
  /**
   * 设置用户信息 
   */
  async onChooseAvatar(event) {
    console.log(event);
    const avatarUrl = event.detail.avatarUrl;
    console.log(avatarUrl);
    if (!avatarUrl) return false;
    wx.showLoading({ title: "正在上传中...", mask: true });
    const { code, data, msg } = await getApp().api.uploadFilePromise(avatarUrl)
    await wx.hideLoading();
    if (code == 1) {
      this.setData({
        avatar: data.uri
      });
    }
  },
  /**
   * 登录
   */
  async login() {
    if (!this.data.isSelected) return this.setData({
      showModel: true
    })
    const { userInfo: { avatarUrl, nickName, gender } } = await wx.getUserProfile({
      desc: '获取用户信息，完善用户资料'
    })
    wx.showLoading({ title: '登录中...', mask: true })
    const ret = await wx.login();
    let object = { code: ret.code, nickname: nickName, headimgurl: avatarUrl }
    const { code, data, msg } = await getApp().api.authLogin(object)
    wx.hideLoading()
    if (code == 1) {
      local.set('userinfo', data)
      local.set('token', data.token)
      if (data.is_new_user == 1) {
        this.setData({
          showLoginPop: true,
        })
      } else {
        wx.switchTab({
          url: '/pages/Home/home/home'
        })
      }
    } else {
      getApp().showToast(msg)
    }
  },
  /**
   * 关闭遮罩层
   */
  onClose(event) {
    const type = event.currentTarget.dataset.type;
    this.setData(type == 'popup' ? { showLoginPop: false } : { showModel: false });
  },
  /**
   * 
   */
  confirm() {
    this.setData({
      isSelected: !this.data.isSelected
    })
  },
  /**
   *切换选中用户协议
   */
  isAgree() {
    this.setData({
      isSelected: !this.data.isSelected
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.config()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})